package cc.mrbird.web.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.system.domain.MidStudent;
import cc.mrbird.system.domain.Student;
import cc.mrbird.web.service.MidStudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class MidStudentController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(MidStudentController.class);

	@Autowired
	private MidStudentService midStudentService;

	@Value("${sys.admin}")
	private String admin;

	@Log("获取五年一贯制考生信息")
	@RequestMapping("mid_student")
	@RequiresPermissions("mid_student:list")
	public String index() {
		return "web/mid_student/mid_student";
	}

	@RequestMapping("midStudent/list")
	@ResponseBody
	public Map<String, Object> midStudentList(QueryRequest request, MidStudent midStudent,String sonUserName) {
		List<MidStudent> list;
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		if(midStudent.getUserName().equalsIgnoreCase(admin) && sonUserName == null){
			midStudent.setUserName("");
			list = this.midStudentService.findMidStudentByCondition(midStudent);
		}else if(midStudent.getUserName().equalsIgnoreCase(admin) && sonUserName != null){
			midStudent.setUserName(sonUserName);
			list = this.midStudentService.findMidStudentByCondition(midStudent);
		}else{
			list = this.midStudentService.findMidStudentByCondition(midStudent);
		}
		PageInfo<MidStudent> pageInfo = new PageInfo<>(list);
		return getDataTable(pageInfo);
	}

	@RequestMapping("midStudent/getMidStudent")
	@ResponseBody
	public ResponseBo getUser(String midStudentId) {
		try {
			MidStudent midStudent = this.midStudentService.findById(midStudentId);
			return ResponseBo.ok(midStudent);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("获取用户信息失败，请联系网站管理员！");
		}
	}

	@Log("新增考生信息")
	@RequiresPermissions("mid_student:add")
	@RequestMapping("midStudent/add")
	@ResponseBody
	public ResponseBo addMidStudent(MidStudent midStudent,String midUserName) {
		try {
			this.midStudentService.addMidStudent(midStudent,midUserName);
			return ResponseBo.ok("新增考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("新增考生失败，请检查考生号或身份证号是否重复！");
		}
	}

	@Log("修改考生信息")
	@RequiresPermissions("mid_student:update")
	@RequestMapping("midStudent/update")
	@ResponseBody
	public ResponseBo updateMidStudent(MidStudent midStudent) {
		try {
			this.midStudentService.updateMidStudent(midStudent);
			return ResponseBo.ok("修改考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("修改考生失败，请联系网站管理员！");
		}
	}

	@Log("删除考生")
	@RequiresPermissions("mid_student:delete")
	@RequestMapping("midStudent/delete")
	@ResponseBody
	public ResponseBo deleteMidStudents(String ids) {
		try {
			this.midStudentService.deleteMidStudents(ids);
			return ResponseBo.ok("删除考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("删除考生失败，请联系网站管理员！");
		}
	}

	@RequestMapping("midStudent/checkStudentId")
	@ResponseBody
	public boolean checkMidStudentId(String midStudentId, String oldmidStudentId) {
		if (StringUtils.isNotBlank(oldmidStudentId) && midStudentId.equalsIgnoreCase(oldmidStudentId)) {
			return true;
		}
		MidStudent result = this.midStudentService.findById(midStudentId);
		if (result != null)
			return false;
		return true;
	}

	@RequestMapping("midStudent/checkIdNumber")
	@ResponseBody
	public boolean checkMidIdNumber(String idNumber, String oldidNumber) {
		if (StringUtils.isNotBlank(oldidNumber) && idNumber.equalsIgnoreCase(oldidNumber)) {
			return true;
		}
		MidStudent result = this.midStudentService.findByIdNumber(idNumber);
		if (result != null)
			return false;
		return true;
	}

}
